
/*---------begin changed by wangwei--------------*/
//the whole file is changed, the original file is named as p_disp_old.c
/*---------end changed---------------------------*/

/* $Header: p_disp.c,v 2.2 01/10/02 10:27:49 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:                                                        */
/* ============                                                        */
/*                                                                     */
/* Author:                                                             */
/*                                                                     */
/* MODULE PURPOSE:                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <string.h>
#include <error.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <i_op.h>
#include <p_status.h>
#include <p_filter.h>
#include <pcb_defs.h>
#include <list_mgmt.h>
#include <ramps.h>         /* needed for status.h */
#include <zone_config.h>   /* needed for status.h */
#include <zone_status.h>   /* needed for status.h */
#include <status.h>        /* needed for display.h */
#include <display.h>
#include <keyboard.h>
#include <iomap.h>         /* needed for leds.h */
#include <leds.h>
#include <regs.h>
#include <oven.h>
#include <exception.h>      /* defs for SetException */
#include <err_handler.h>    /* defs for SetException */
#include <detector.h>       /* DetGasShutdown proto */
#include <beep.h>
#include <zone_ui.h>
#include <det_ui.h>
#include <p_ui.h>

/*------begin changed by wangwei----------------*/
#include <sys/time.h>
/*------end changed by wangwei------------------*/
/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/

#include "proto.h"

#define pFRONT_INLET         GetFrontInletPtr()
#define pBACK_INLET          GetBackInletPtr()
#define pFRONT_DETECTOR       GetFrontDetPtr()
#define pBACK_DETECTOR       GetBackDetPtr()

/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


U_INT16 num_fatal_errors;



const BIT8 * const Pneu_Death_Msgs[] = {
                                          (BIT8 *) "PCB Cmd Error:      \r",
                                          (BIT8 *) "PCB Checksum Error  \r",
                                          (BIT8 *) "Non Responding PCB  \r",
                                          (BIT8 *) "Bad Gas Selected    \r",
                                          (BIT8 *) "Bad Inlet Type      \r",
                                          (BIT8 *) "Bad Control Mode    \r",
                                          (BIT8 *) "Bad Sensor Range    \r",
                                          (BIT8 *) "Bad Sensor Range    \r",
                                          (BIT8 *) "Bad Detector Type   \r",
                                          (BIT8 *) "Bad Column Source   \r",
                                          (BIT8 *) "Pending Buffer      \r",
                                          (BIT8 *) "No Buffers Available\r",
                                          (BIT8 *) "Cant Get Initial Env\r"  };




/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/



/*****************************/
/*   STATIC VARIABLES        */
/*****************************/

static void Gas2DrivenFDETShutDown () ;
static void Gas2DrivenBDETShutDown () ;

/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Called by status monitor to check health of pcb2           */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void CheckForDeadPcb( void )

/**end_proto**/
{

    printf("call CheckForDeadPcb\n");

}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void ClearPneuExceptions( void )

/**end_proto**/
{

    printf("call ClearPneuExceptions\n");

}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void ShutDownSmallZoneThermal ()

/**end_proto**/
{

    printf("call ShutDownSmallZoneThermal\n");
}

/**begin_proto**/

void LowTCDRefCheck(pDETECTOR_STATUS status )

/**end_proto**/
{
    POSITION p;     /* FRONT | BACK */

    if (
      (status->Config->Type == EPC_TCD                                        ) &&
      (GetTCDFilament(p = status == pFRONT_DETECTOR ? FRONT: BACK ) == ON   ) &&
      (status->Gas_2.Filtered_Current_Flow < status->Min_Gas_2_Flow_Rate >> 1 )
      )
    {
      if (p == FRONT)
      {
        Gas2DrivenFDETShutDown();
      }
      else
      {
        Gas2DrivenBDETShutDown() ;
      }  ;
    }
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */

/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void HandleShutdowns ( U_INT16 shutdown )

/**end_proto**/
{
   // printf("call HandleShutdowns\n");
     U_INT16 new_shutdowns;
    U_INT16 mask;
if(shutdown != 0)
printf("shutdown = %04x\n",shutdown);

    /* the xor finds changes, the and limits the changes to shutdowns, not restarts */

    new_shutdowns = shutdown & ( shutdown ^ Inst_Status.Pneu.Last_Shutdown_Mask ) ;

    Inst_Status.Pneu.Last_Shutdown_Mask         = shutdown;
    Inst_Status.Pneu.Channels_Reported_Shutdown = shutdown;  /* channels currently shutdown */

    if ( new_shutdowns == 0 ) return;                /* if no new shutdowns, just leave */


    /* hydrogen saftey shutdown */


    mask = 0;  /* find all channels using hydrogen */

    if ( Inst_Config.Pneu.Frnt_Inlet.Gas_Type == H2 ) mask |= (FRNT_INLET_PRESSURE_MASK | FRNT_INLET_FLOW_MASK);
    if ( Inst_Config.Pneu.Back_Inlet.Gas_Type == H2 ) mask |= (BACK_INLET_PRESSURE_MASK | BACK_INLET_FLOW_MASK);
    if ( Inst_Config.Pneu.Aux_3.Gas_Type == H2 )      mask |= AUX_3_MASK;
    if ( Inst_Config.Pneu.Aux_4.Gas_Type == H2 )      mask |= AUX_4_MASK;
    if ( Inst_Config.Pneu.Aux_5.Gas_Type == H2 )      mask |= AUX_5_MASK;

    if ( (shutdown & mask) != 0 )
    {
      if ( ((new_shutdowns & mask) != 0 ) && (!TestForException( HYDROGEN_SHUTDOWN )) )
      {
        TurnOvenOff();
      }

      SetNewException( HYDROGEN_SHUTDOWN, 0, 0 );
    }




    /* turn oven off if column source shuts down.     */
    /* columns die if left in a hot oven with no flow */

    mask = 0;

    switch ( Inst_Config.Pneu.Column_1.Source )
    {
    case FRNT_INLET: mask    |= (FRNT_INLET_PRESSURE_MASK | FRNT_INLET_FLOW_MASK);
      break;
    case BACK_INLET: mask    |= (BACK_INLET_PRESSURE_MASK | BACK_INLET_FLOW_MASK);
      break;
    case AUX_3:      mask    |= AUX_3_MASK;
      break;
    case AUX_4:      mask    |= AUX_3_MASK;
      break;
    case AUX_5:      mask    |= AUX_3_MASK;
      break;
    }

    switch ( Inst_Config.Pneu.Column_2.Source )
    {
    case FRNT_INLET: mask    |= (FRNT_INLET_PRESSURE_MASK | FRNT_INLET_FLOW_MASK);
      break;
    case BACK_INLET: mask    |= (BACK_INLET_PRESSURE_MASK | BACK_INLET_FLOW_MASK);
      break;
    case AUX_3:      mask    |= AUX_3_MASK;
      break;
    case AUX_4:      mask    |= AUX_3_MASK;
      break;
    case AUX_5:      mask    |= AUX_3_MASK;
      break;
    }

    if ( (shutdown & mask) != 0 )
    {
      if ((new_shutdowns & mask) != 0 )
      {
        RequestOvenShutOffByColumn();
        ShutDownSmallZoneThermal();
      }
    }







    if ( Inst_Status.Pneu.Halt_On_Shutdown ) PneuFatalError( 3, 0xc0ed );

    if ((new_shutdowns & FRNT_INLET_PRESSURE_MASK) != 0 )
    {
      Inst_Status.Pneu.Frnt_Inlet.Shutdown = TRUE;
      SetNewException( FRNT_INLET_PRES_SHUTDOWN,0,0 );
    }

    if ((new_shutdowns & FRNT_INLET_FLOW_MASK)     != 0 )
    {
      Inst_Status.Pneu.Frnt_Inlet.Shutdown = TRUE;
      SetNewException( FRNT_INLET_FLOW_SHUTDOWN,0,0 );
    }

    if ((new_shutdowns & BACK_INLET_PRESSURE_MASK) != 0 )
    {
      Inst_Status.Pneu.Back_Inlet.Shutdown = TRUE;
      SetNewException( BACK_INLET_PRES_SHUTDOWN,0,0 );
    }

    if ((new_shutdowns & BACK_INLET_FLOW_MASK)     != 0 )
    {
      Inst_Status.Pneu.Back_Inlet.Shutdown = TRUE;
      SetNewException( BACK_INLET_FLOW_SHUTDOWN,0,0 );
    }


    if ( (new_shutdowns & FRNT_DETECTOR_GAS_1_MASK) != 0 )
    {
      if (!DetIsTCDorAnyECD( pFRONT_DETECTOR ) )
      {
        SetNewException( FRNT_DET_GAS_1_SHUTDOWN,0,0 );
        Inst_Status.Pneu.Frnt_Detector.Gas_1.Shutdown = TRUE;
        Inst_Status.Pneu.Frnt_Detector.Gas_2.On_Off = OFF;
        Inst_Status.Pneu.Frnt_Detector.Gas_3.On_Off = OFF;
        Inst_Status.Pneu.Frnt_Detector.Shutdown = TRUE;
        DetGasShutdown( FRONT, 1 );
      }
    }

    if ( ((new_shutdowns & FRNT_DETECTOR_GAS_2_MASK) != 0) && (!DetIsAnyUECD( pFRONT_DETECTOR )) )
    {
      Gas2DrivenFDETShutDown() ;
    }

    if ( (new_shutdowns & FRNT_DETECTOR_GAS_3_MASK) != 0 )
    {
      SetNewException( FRNT_DET_GAS_3_SHUTDOWN,0,0 );
      Inst_Status.Pneu.Frnt_Detector.Gas_3.Shutdown = TRUE;
      Inst_Status.Pneu.Frnt_Detector.Gas_1.On_Off = OFF;
      Inst_Status.Pneu.Frnt_Detector.Gas_2.On_Off = OFF;
      Inst_Status.Pneu.Frnt_Detector.Shutdown = TRUE;
      DetGasShutdown( FRONT, 3 );
    }


    if ( (new_shutdowns & BACK_DETECTOR_GAS_1_MASK) != 0 )
    {
      if (!DetIsTCDorAnyECD( pBACK_DETECTOR ) )
      {
        SetNewException( BACK_DET_GAS_1_SHUTDOWN,0,0 );
        Inst_Status.Pneu.Back_Detector.Gas_1.Shutdown = TRUE;
        Inst_Status.Pneu.Back_Detector.Gas_2.On_Off = OFF;
        Inst_Status.Pneu.Back_Detector.Gas_3.On_Off = OFF;
        Inst_Status.Pneu.Back_Detector.Shutdown = TRUE;
        DetGasShutdown( BACK, 1 );
      }
    }

    if ( ((new_shutdowns & BACK_DETECTOR_GAS_2_MASK) != 0) && (!DetIsAnyUECD( pBACK_DETECTOR )) )
    {
      Gas2DrivenBDETShutDown()  ;
    }

    if ( (new_shutdowns & BACK_DETECTOR_GAS_3_MASK) != 0 )
    {
      SetNewException( BACK_DET_GAS_3_SHUTDOWN,0,0 );
      Inst_Status.Pneu.Back_Detector.Gas_3.Shutdown = TRUE;
      Inst_Status.Pneu.Back_Detector.Gas_1.On_Off = OFF;
      Inst_Status.Pneu.Back_Detector.Gas_2.On_Off = OFF;
      Inst_Status.Pneu.Back_Detector.Shutdown = TRUE;
      DetGasShutdown( BACK, 3 );
    }

    if ( (new_shutdowns & AUX_3_MASK)               != 0 )
    {
      SetNewException( PNEU_AUX_3_SHUTDOWN,0,0 );
      Inst_Status.Pneu.Aux.Aux_3.Shutdown = TRUE;
    }

    if ( (new_shutdowns & AUX_4_MASK)               != 0 )
    {
      SetNewException( PNEU_AUX_4_SHUTDOWN,0,0 );
      Inst_Status.Pneu.Aux.Aux_4.Shutdown = TRUE;
    }

    if ( (new_shutdowns & AUX_5_MASK)               != 0 )
    {
      SetNewException( PNEU_AUX_5_SHUTDOWN,0,0 );
      Inst_Status.Pneu.Aux.Aux_5.Shutdown = TRUE;
    }


}

void MonitorEnvNoise ( pQUEUE_ENTRY response_buffer )
{
    U_INT16 i;
    U_INT16 actual;

    for ( i = 0 ; i < NUM_ENV_CHANNELS ; i++ )
    {
      actual = response_buffer->PCB_Response[ FIRST_CHANNEL_INDEX + i ];

      if ( Inst_Status.Pneu.ENV_MIN[i] > actual ) Inst_Status.Pneu.ENV_MIN[i] = actual;
      if ( Inst_Status.Pneu.ENV_MAX[i] < actual ) Inst_Status.Pneu.ENV_MAX[i] = actual;

      if ( Inst_Status.Pneu.RMS_Counts == 0 )
      {
        JamFilter( Inst_Status.Pneu.Env_Filters[ i ], actual );
      }
      else
      {
        FilterValue( Inst_Status.Pneu.Env_Filters[ i ], actual );
      }

    }

    if ( Inst_Status.Pneu.RMS_Counts >=  Inst_Status.Pneu.RMS_Interval )
    {
      for ( i = 0 ; i < NUM_ENV_CHANNELS ; i++ )
      {
        Inst_Status.Pneu.ENV_MAX_MIN[i] = Inst_Status.Pneu.ENV_MAX[i]-Inst_Status.Pneu.ENV_MIN[i];
        if ( i == ( PNEU_ADC_OFFSET_INDEX - FIRST_CHANNEL_INDEX ))
        {
          Inst_Status.Pneu.Stable_Env[i]  =                    Inst_Status.Pneu.Env_Filters[ i ];
        }
        else
        {
          Inst_Status.Pneu.Stable_Env[i]  = SubtractAdcOffset( Inst_Status.Pneu.Env_Filters[ i ] );
        }
        Inst_Status.Pneu.ENV_MIN[i] = 0xffff;
        Inst_Status.Pneu.ENV_MAX[i] = 0x0000;
      }
      Inst_Status.Pneu.RMS_Counts = 0;
    }

}

void MonitorDutyCycles( pQUEUE_ENTRY response_buffer )
{
    U_INT16 i;

    for ( i = 0 ; i < NUM_PNEU_CHANNELS ; i++ )
    {
      Inst_Status.Pneu.Actual_Duty_Cycles[i] = response_buffer->PCB_Response[ FIRST_CHANNEL_INDEX + i ];
    }
}

/**begin_proto**/

void MonitorSensorNoise( void )

/**end_proto**/
{
    U_INT16 i;
    U_INT16 j;
    U_INT16 min;
    U_INT16 max;
    U_INT16 filter;
    U_INT16 reading;

    for ( i = 0 ; i < NUM_PNEU_CHANNELS ; i++ )
    {
      min = 0xffff;
      max = 0x0000;
      filter = 0;

      for ( j = 0 ; j < NUM_STORED_READINGS ; j++ )
      {
        reading = Inst_Status.Pneu.Raw_Readings[ j ][ i ];

        if ( j == 0 )
        {
          JamFilter( filter, reading );
        }
        else
        {
          FilterValue( filter, reading );
        }

        if ( min > reading ) min = reading;
        if ( max < reading ) max = reading;
      }



      Inst_Status.Pneu.MAX_MIN[i] = max - min;

      Inst_Status.Pneu.Stable_Volts[ i ] = SubtractAdcOffset( filter );
    }

    /* now, reset the index to re-enable data collection */

    Inst_Status.Pneu.Raw_Rdg_Index = 0;


}



void PreShutdownWarning( pQUEUE_ENTRY response_buffer )
{
    U_INT16 i;
    U_INT16 max_count;
    BIT8    shutdown;
    ON_OFF  on;

    max_count = 0;


    /* find the largest icap sum for channels that ar ON and NOT already shutdown */

    for ( i = 0 ; i < NUM_PNEU_CHANNELS ; i++ )
    {
      Inst_Status.Pneu.Iscc[i] = response_buffer->PCB_Response[ FIRST_CHANNEL_INDEX + i ];

      switch ( i )
      {
      case 0:
      case 1:  shutdown = Inst_Status.Pneu.Frnt_Inlet.Shutdown;
        on       = Active_Wkfile.Pneu.Frnt_Inlet.On_Off;
        break;

      case 2:
      case 3:  shutdown = Inst_Status.Pneu.Back_Inlet.Shutdown;
        on       = Active_Wkfile.Pneu.Back_Inlet.On_Off;
        break;

      case 4:  shutdown = Inst_Status.Pneu.Frnt_Detector.Shutdown;
        on       = Active_Wkfile.Pneu.Frnt_Detector.Gas_1.On_Off;
        break;

      case 5:  shutdown = Inst_Status.Pneu.Frnt_Detector.Shutdown;
        on       = Active_Wkfile.Pneu.Frnt_Detector.Gas_2.On_Off;
        break;

      case 6:  shutdown = Inst_Status.Pneu.Frnt_Detector.Shutdown;
        on       = Active_Wkfile.Pneu.Frnt_Detector.Gas_3.On_Off;
        break;

      case 7:  shutdown = Inst_Status.Pneu.Back_Detector.Shutdown;
        on       = Active_Wkfile.Pneu.Back_Detector.Gas_1.On_Off;
        break;

      case 8:  shutdown = Inst_Status.Pneu.Back_Detector.Shutdown;
        on       = Active_Wkfile.Pneu.Back_Detector.Gas_2.On_Off;
        break;

      case 9:  shutdown = Inst_Status.Pneu.Back_Detector.Shutdown;
        on       = Active_Wkfile.Pneu.Back_Detector.Gas_3.On_Off;
        break;

      case 10: shutdown = Inst_Status.Pneu.Aux.Aux_3.Shutdown;
        on       = Active_Wkfile.Pneu.Aux_3.On_Off;
        break;

      case 11: shutdown = Inst_Status.Pneu.Aux.Aux_4.Shutdown;
        on       = Active_Wkfile.Pneu.Aux_4.On_Off;
        break;

      case 12: shutdown = Inst_Status.Pneu.Aux.Aux_5.Shutdown;
        on       = Active_Wkfile.Pneu.Aux_5.On_Off;
        break;

      default: shutdown = FALSE;  on = OFF;
      }

      if ( ( max_count < response_buffer->PCB_Response[ FIRST_CHANNEL_INDEX + i ] ) && !shutdown  && (on == ON))
      {
        max_count = response_buffer->PCB_Response[ FIRST_CHANNEL_INDEX + i ];
      }
    }


#define TICKS_PER_BEEP   0x1800
#define MIN(a,b)         (((a)<(b))?(a):(b))
#define MAX(a,b)         (((a)>(b))?(a):(b))


    /* if counter has passed a threshold since we last made noise, make more! */
    /*   saving the value of the counter when we made the noise.              */
    /*                                                                        */
    /* and if the counter has lowered, be silent and save this new value      */

    /* there are some funny things going on to keep iq from beeping when we   */
    /* have our first iteration past tick per beep.  it seems that we would   */
    /* be beeping when an inlet is trying to pressurize with a low flow.      */
    /* we will just delay the beep until the beep count is greater than 2     */

    /* the outer if stmt allows beeping to continue for a second channel about   */
    /* to shutdown after a first channel has declared shutdown.  the else clause */
    /* only executes after a first has shutdown, not continually                 */

    if ( max_count > Inst_Status.Pneu.Last_Icap_Ctr )  /** still upcounting **/
    {
      if ( (max_count - Inst_Status.Pneu.Last_Icap_Ctr ) > TICKS_PER_BEEP )
      {
        if ( ( max_count / TICKS_PER_BEEP ) >= 2 )  /* skip first beep */
        {
          if (GetWarningBeepEnabled())
          {
            BeepBeep(0, 300, (max_count/TICKS_PER_BEEP) -1, 2,  /* decrement beep count */
              512,   0,  75,
              1024,  75, 150 );
          }
        }

        Inst_Status.Pneu.Last_Icap_Ctr = max_count - ( max_count % TICKS_PER_BEEP );
      }
    }
    else
    {
      Inst_Status.Pneu.Last_Icap_Ctr = max_count - ( max_count % TICKS_PER_BEEP );
    }



}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void SaveSensorReadings( pQUEUE_ENTRY buffer )

/**end_proto**/
{
    U_INT16 *source;
    U_INT16 *dest;
    U_INT16 *last;

    /* save every fifth reading @ 50 Hz for a two second update rate */

    if ( (++Inst_Status.Pneu.RMS_Counts % 5) != 0 ) return;

    /* this will only allow the readings to be saved when any calculations */
    /* based on these readings have completed.  when calculations are done */
    /* the value will be reset to zero                                     */

    if ( Inst_Status.Pneu.Raw_Rdg_Index < NUM_STORED_READINGS )
    {
      dest   = &Inst_Status.Pneu.Raw_Readings[ Inst_Status.Pneu.Raw_Rdg_Index ] [ 0 ];
      last   = &Inst_Status.Pneu.Raw_Readings[ Inst_Status.Pneu.Raw_Rdg_Index ] [ NUM_PNEU_CHANNELS ];
      source = &buffer->PCB_Response[ FIRST_CHANNEL_INDEX ];

      while ( dest < last )
      {
        *dest++ = *source++;
      }

      if ( ++Inst_Status.Pneu.Raw_Rdg_Index == NUM_STORED_READINGS )
      {
        SendSensorNoiseRequest();
      }
    }
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

/**begin_proto**/

void PneuDisplay (void)

/**end_proto**/
{
//    printf("call PneuDisplay\n");
 
    U_INT8  num_buffers_read;
    pQUEUE_ENTRY response_buffer;
    U_INT16 *word_ptr;
     U_INT32 time_this_invocation;
static U_INT8  ctr;
static U_INT32 sum;

    struct timeval start;
    struct timeval end;

//     gettimeofday(&start,NULL);
    while ( 1 )
    {

pthread_mutex_lock(&recvqueue_mut); 
      GetBuffer(response_buffer, &Pneu_Resp_Queue);
pthread_mutex_unlock(&recvqueue_mut); 

      /* if all received buffers have been processed, exit */
      if ( response_buffer == (pQUEUE_ENTRY)NULL ) break;

      ActualEqualsSetpt ( response_buffer );

      if ( response_buffer->Return_Queue != NULL )
      {
        /* queue up the response to the background process requesting it */
/*
printf("response_buffer->PCB_Command[0] = %d\n",response_buffer->PCB_Command[0]);
printf("response_buffer->Return_Queue = %d\n",response_buffer->Return_Queue);
printf("Stat_Mon_Queue=%d\n",&Stat_Mon_Queue);
printf("Disp_Mon_Queue=%d\n",&Disp_Mon_Queue);
printf("Keyboard_Cmd_Proc_Queue=%d\n",&Keyboard_Cmd_Proc_Queue);
printf("DataComm_Cmd_Proc_Queue=%d\n",&DataComm_Cmd_Proc_Queue);
printf("RS_232_Cmd_Proc_Queue=%d\n",&RS_232_Cmd_Proc_Queue);
printf("Gas_Task_Queue=%d\n",&Gas_Task_Queue);
printf("Seq_Task_Queue=%d\n",&Seq_Task_Queue);
printf("Clk_Task_Queue=%d\n",&Clk_Task_Queue);
*/
        LinkBufferToTail(response_buffer, response_buffer->Return_Queue);
        continue;
      }
 //     printf("response_buffer->PCB_Command[0]=%04x\n",response_buffer->PCB_Command[0]);
      /*******  after this we are dealing only with foreground command responses ********/

      HandleShutdowns ( response_buffer->PCB_Response[SHUTDOWN_MASK_INDEX] );
      switch ( response_buffer->PCB_Command[0] )
      {
      case RESTART_CHANNELS:    CheckIfRetryNeeded ( response_buffer );
        break;

      case REQUEST_ENVIRONMENT: UpdateEnvironmentals ( response_buffer );
        MonitorEnvNoise      ( response_buffer );
        break;

      case REQUEST_ICAP_CTRS:   PreShutdownWarning ( response_buffer );
        break;

      case READ_DUTY_CYCLES:    MonitorDutyCycles( response_buffer );
        break;

      case DOWNLOAD_SETPOINTS:  // MonitorSensorNoise( response_buffer ); 
        SaveSensorReadings( response_buffer );

        word_ptr = &response_buffer->PCB_Response[ FIRST_CHANNEL_INDEX ];

        Inst_Status.Pneu.Frnt_Inlet.Pres_Adc_Actual     = *word_ptr++;
        Inst_Status.Pneu.Frnt_Inlet.Flow_Adc_Actual     = *word_ptr++;
        Inst_Status.Pneu.Back_Inlet.Pres_Adc_Actual     = *word_ptr++;
        Inst_Status.Pneu.Back_Inlet.Flow_Adc_Actual     = *word_ptr++;
        Inst_Status.Pneu.Frnt_Detector.Gas_1.Adc_Actual = *word_ptr++;
        Inst_Status.Pneu.Frnt_Detector.Gas_2.Adc_Actual = *word_ptr++;
        Inst_Status.Pneu.Frnt_Detector.Gas_3.Adc_Actual = *word_ptr++;
        Inst_Status.Pneu.Back_Detector.Gas_1.Adc_Actual = *word_ptr++;
        Inst_Status.Pneu.Back_Detector.Gas_2.Adc_Actual = *word_ptr++;
        Inst_Status.Pneu.Back_Detector.Gas_3.Adc_Actual = *word_ptr++;
        Inst_Status.Pneu.Aux.Aux_3.Adc_Actual           = *word_ptr++;
        Inst_Status.Pneu.Aux.Aux_4.Adc_Actual           = *word_ptr++;
        Inst_Status.Pneu.Aux.Aux_5.Adc_Actual           = *word_ptr++;
        num_buffers_read++;
        break;
      }

      LinkBufferToTail(response_buffer, &Pneu_Free_Pool);


    } /* while buffers exist */

   if ( num_buffers_read == 0 )
   {
         if ( Inst_Status.Pneu.Block_Foreground_Interrupts )
         {
            INTERRUPTS_ON;
         }
         return;
   }

     if ( !DoingMethodInstall() )   /* only here to reduce loading during method load */
     {

          switch ( Inst_Status.Pneu.What_Now )
          {

                 /*********************************************/
                 /**  Do the frnt inlet                      **/
                 /**                                         **/
                 /*********************************************/

                case DO_FRNT_INLET:

                     if ( EpcInlet( &Inst_Status.Pneu.Frnt_Inlet ) )
                     {

/*                      switch ( GetInletType(pFRONT_INLET)    )               */
                        switch ( VIGetInletType(pFRONT_INLET)  )
                        {
/*                          case VOLATILES:                                    */
                            case SS:
                            case CIS3:
                            case CIS4:
                            case PTV: CalcInletCurrentPres      ( &Inst_Status.Pneu.Frnt_Inlet );
                                      CalcSSInletCurrentFlows   ( &Inst_Status.Pneu.Frnt_Inlet );
                                      break;

                            case PP:
                            case SIMDIST:
                            case ACI: CalcInletCurrentPres      ( &Inst_Status.Pneu.Frnt_Inlet );
                                      CalcPPInletCurrentFlows   ( &Inst_Status.Pneu.Frnt_Inlet );
                                      break;

                            case COC: CalcInletCurrentPres      ( &Inst_Status.Pneu.Frnt_Inlet );
                                      CalcCOCInletCurrentFlows  ( &Inst_Status.Pneu.Frnt_Inlet );
                                      break;

                            case JIB: CalcJackInTheBoxFP   ( &Inst_Status.Pneu.Frnt_Inlet );
                                      break;

                            default: ;
                        }

                     }

/*
                     if (( Inst_Status.Pneu.Frnt_Inlet.Miser_Mode == TRUE ) &&
                         ( Active_Wkfile.Pneu.Frnt_Inlet.On_Off == ON )     &&
                         ( Inst_Config.Pneu.Frnt_Inlet.Functional == TRUE ))    { TURN_FRONT_MISER_LED_ON  }
                     else                                                       { TURN_FRONT_MISER_LED_OFF };
*/
                     break;


                 /*********************************************/
                 /**  Do the back inlet                      **/
                 /**                                         **/
                 /*********************************************/

                case DO_BACK_INLET:

                     if ( EpcInlet( &Inst_Status.Pneu.Back_Inlet ) )
                     {

/*                      switch ( GetInletType(pBACK_INLET)    )               */
                        switch ( VIGetInletType(pBACK_INLET)  )
                        {
/*                          case VOLATILES:                                    */
                            case SS:
                            case CIS3:
                            case CIS4:
                            case PTV: CalcInletCurrentPres      ( &Inst_Status.Pneu.Back_Inlet );
                                      CalcSSInletCurrentFlows   ( &Inst_Status.Pneu.Back_Inlet );
                                      break;

                            case PP:
                            case SIMDIST:
                            case ACI: CalcInletCurrentPres      ( &Inst_Status.Pneu.Back_Inlet );
                                      CalcPPInletCurrentFlows   ( &Inst_Status.Pneu.Back_Inlet );
                                      break;

                            case COC: CalcInletCurrentPres      ( &Inst_Status.Pneu.Back_Inlet );
                                      CalcCOCInletCurrentFlows  ( &Inst_Status.Pneu.Back_Inlet );
                                      break;

                            case JIB: CalcJackInTheBoxFP        ( &Inst_Status.Pneu.Back_Inlet );
                                      break;

                            default: ;
                        }

                     }
/*
                     if (( Inst_Status.Pneu.Back_Inlet.Miser_Mode == TRUE ) &&
                         ( Active_Wkfile.Pneu.Back_Inlet.On_Off == ON )     &&
                         ( Inst_Config.Pneu.Back_Inlet.Functional == TRUE ))    { TURN_BACK_MISER_LED_ON  }
                     else                                                       { TURN_BACK_MISER_LED_OFF };
*/
                     break;

                 /*********************************************/
                 /**  Do the front detector                  **/
                 /**                                         **/
                 /*********************************************/

                case DO_FRNT_DET: if ( EpcDetector( &Inst_Status.Pneu.Frnt_Detector ) )
                                  {
                                      CalcDetGasFP(&Inst_Status.Pneu.Frnt_Detector );
                                  }
                                  break;

                 /*********************************************/
                 /**  Do the back detector                   **/
                 /**                                         **/
                 /*********************************************/


                case DO_BACK_DET: if ( EpcDetector( &Inst_Status.Pneu.Back_Detector ) )
                                  {
                                      CalcDetGasFP(&Inst_Status.Pneu.Back_Detector );
                                  }
                                  break;


                 /*********************************************/
                 /**  Do the auxes                           **/
                 /**                                         **/
                 /*********************************************/

                 case DO_AUXES: CalcAuxPressure ( &Inst_Status.Pneu.Aux.Aux_3, &Inst_Status.Pneu.Aux );
                                CalcAuxPressure ( &Inst_Status.Pneu.Aux.Aux_4, &Inst_Status.Pneu.Aux );
                                CalcAuxPressure ( &Inst_Status.Pneu.Aux.Aux_5, &Inst_Status.Pneu.Aux );
                                break;
          }


     } /* doing method install */


/*
     out_ctr_8khz = *(U_INT8 *)PACNT;

     if ( out_ctr_8khz < ctr_8khz)
     {
          time_this_invocation = (1 + out_ctr_8khz + ( 0xff - ctr_8khz ) ) *125;
     }
     else
     {
          time_this_invocation = ( 1 + out_ctr_8khz - ctr_8khz ) * 125;
     }
*/

//     gettimeofday(&end,NULL);
     time_this_invocation = 0;//10000*(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec)/100;

     if ( ctr-- == 0 )
     {
         Inst_Status.Pneu.Avg_Disp_Time = sum / 250;
         ctr = 250;
         sum = 0;
     }
     else
     {
         sum += time_this_invocation;
     }

     if ( Inst_Status.Pneu.Peak_Disp_Time < time_this_invocation ) Inst_Status.Pneu.Peak_Disp_Time = time_this_invocation;


     if ( Inst_Status.Pneu.Block_Foreground_Interrupts )
     {
         INTERRUPTS_ON;
     }


     Inst_Status.Pneu.Auto_Cal_Timer--;  /* decrement a timer used to delay auto cal */

}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void FormatHexShort ( U_INT16  num, char *string )

/**end_proto**/
{
    U_INT8 digit;
    INT8   i;

    for ( i = 0 ; i < 4 ; i++ )
    {

        digit = num & 0xf;
        num   = num >> 4;

        if ( digit <= 9 ) *string-- = digit + '0';
        else              *string-- = digit + 'a' - 10;

    }
}

/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
/**begin_proto**/

void PneuFatalError (U_INT16 source, U_INT16 reason )

/**end_proto**/
{

       /*  source   0:   reason is a local error number       */
       /*           1:   reason is a pcb error return number  */
       /*           2:   scram pneumatics, reason is local    */
       /*           3:   scram pneumatics, reason is pcb #    */

int i;


      char message[] =
         /*  0----+----1----+---    - 2----+----3----+----4 ----+----5----+----6- ---+----7----+----8--- */
            "Pneu Error        \374 \r                    \r                    \r                     \r\000";

//   printf("PneuFatalError--source:%d---reason:%d----!\n",source,reason);
/*
   if (( source == 2 ) || ( source == 3 ))
   {
        Inst_Status.Pneu.Pneu_Alive = FALSE;
//        __asm(" tst.b ($4c000e).l" );
   }


   if (( source == 0 ) || ( source == 2 )) (void)strncpy( &message[63], (const char *)Pneu_Death_Msgs[reason], 21 );

   if (( source == 1 ) || ( source == 3 ))
   {
       (void)strncpy( &message[63], (const char *)Pneu_Death_Msgs[0], 21 );
       FormatHexShort ( reason, &message[80] );
   }

   FormatHexShort ( ++num_fatal_errors, &message[15] );

   if ( Initialization_Complete )
   {
       PauseMonitor();
       DisplayRamString( (BIT8 *)message );
   }
   else
   {
       for ( i = 0 ; i <= 83; i++ )
       {
           while (  (*(BIT8 *)PACTL) & 0x08 ) {}; //wait if display busy 
           DISPLAY = message[i];
       }
   }

*/
}

static void Gas2DrivenFDETShutDown ()
{
  printf("call Gas2DrivenFDETShutDown\n");
}

static void Gas2DrivenBDETShutDown ()
{
  printf("call Gas2DrivenBDETShutDown\n");
}
